package org.check.UI;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.List;

import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EtchedBorder;

import org.check.SQL.ConnectionPool;
import org.check.UI.ConfigManager;
import org.check.UI.QuickConfigPanel;
import org.check.Util.XmlParser;
import org.check.VO.DBConnVO;

import net.miginfocom.swing.MigLayout;
import javax.swing.JTextField;

public class DBConnSelectPanel extends JPanel {

	
	private final String conns_config = "conns_config.xml";
	private JComboBox combo_selectConfig;
	private List<DBConnVO> dbConnVOs;
	private String[] menu;
	private JTextField text_vpnInfo;

	/**
	 * Create the panel.
	 * @param quickConfigPanel 
	 * @param exportXlsPanel 
	 * @param dbConnVOs 
	 */
	public DBConnSelectPanel(final List<DBConnVO> dbConnVOsIn, final QuickConfigPanel quickConfigPanel) {
		this.dbConnVOs = dbConnVOsIn;
		setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
		setBackground(new Color(173, 216, 230));
		setLayout(
				new MigLayout("", "[95.00px][114.00px:n][100px:n][56.00px:n][grow][100px:n:100px]", "[23px]"));

		combo_selectConfig = new JComboBox();
		combo_selectConfig.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				quickConfigPanel.repaintQuickConfig(combo_selectConfig.getSelectedIndex());
				text_vpnInfo.setText(dbConnVOs.get(combo_selectConfig.getSelectedIndex()).getVpnInfo());
				repaint();
			}
		});

		menu = XmlParser.getNames(dbConnVOs);
		ComboBoxModel menuModel = new DefaultComboBoxModel(menu);
		combo_selectConfig.setModel(menuModel);
		add(combo_selectConfig, "cell 1 0,growx,aligny center");

		JButton button_testConn = new JButton("测试连接");
		button_testConn.addActionListener(new ActionListener() {
			private ConnectionPool connPool;

			@Override
			public void actionPerformed(ActionEvent e) {
				try {
					DBConnVO dbConnVO = quickConfigPanel.loadQuickConfig(getDbConnVO(combo_selectConfig.getSelectedIndex()));
					connPool = new ConnectionPool(dbConnVO);
				} catch (Exception dbe) {
					dbe.printStackTrace();
					JOptionPane.showMessageDialog(null, "连接失败", "提示", JOptionPane.PLAIN_MESSAGE);
					connPool=null;
					return;
				}
				JOptionPane.showMessageDialog(null, "连接成功", "提示", JOptionPane.PLAIN_MESSAGE);
				connPool=null;
			}
		});

		button_testConn.setToolTipText("测试连接是否成功");
		add(button_testConn, "cell 2 0,growx,aligny top");

		

		JLabel label_3 = new JLabel("选择数据库配置");
		add(label_3, "cell 0 0,growx,aligny center");

		JButton button_5 = new JButton("管理配置");
		button_5.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ConfigManager configManager = new ConfigManager(dbConnVOs);
				configManager.getDialog().setVisible(true);
				reloadConfigs();
			}
		});
		
		JLabel lblVpn = new JLabel("VPN信息：");
		add(lblVpn, "cell 3 0,alignx trailing");
		
		String vpnInfo = dbConnVOs.get(combo_selectConfig.getSelectedIndex()).getVpnInfo();
		text_vpnInfo = new JTextField(vpnInfo);
		add(text_vpnInfo, "cell 4 0,growx");
		text_vpnInfo.setColumns(10);
		button_5.setToolTipText("建议在研发人员的帮助下进行管理");
		add(button_5, "cell 5 0,growx,aligny top");
	}
	
	protected void reloadConfigs() {
		dbConnVOs = XmlParser.generateDBVOList(conns_config);
		menu = XmlParser.getNames(dbConnVOs);
		ComboBoxModel menuModel = new DefaultComboBoxModel(menu);
		combo_selectConfig.setModel(menuModel);
		combo_selectConfig.repaint();
	}
	
	
	public List<DBConnVO> getDbConnVOs() {
		return dbConnVOs;
	}

	public String[] getMenu() {
		return menu;
	}

	public DBConnVO getDbConnVO(String configName) {
		// TODO Auto-generated method stub
		for (DBConnVO dbConnVO : dbConnVOs) {
			if (XmlParser.unicodeDecode(dbConnVO.getName()).equals(configName)) {
				return dbConnVO;
			}
		}
		return null;
	}

	public DBConnVO getDbConnVO(int index) {
		return dbConnVOs.get(index);
	}
	

}
